const app = getApp();
import { getGoodsList } from "../../../api";
Page({
  data: {
    topCity: '全国',
    productList: [],
    searchValue: '',
    isSortTop: false,
    isFilter: false,
    sortTabs: [
      { id: 0, title: '默认排序' },
      { id: 1, title: '价格降序' },
      { id: 2, title: '价格升序' },
      { id: 3, title: '最新上架' }
    ],
    tabsTitle: "默认排序",
    activeTabId_sort: 0,
    logisticType: '',
    logisticTabs: [
      { id: 0, title: '包邮', type: 1 },
      { id: 1, title: '到付', type: 2 },
    ],
    activeTabId_logistic: -1,
    lowestRent: '',
    highestRent: '',
    cateId: '',
    orderby_name: 'id',
    sort: 'desc',
    convey_type: '',
    min_price: '',
    max_price: ''
  },
  onLoad(option) {
      const launchOptions = my.getLaunchOptionsSync();
      if(launchOptions.query&&launchOptions.query.cateId&&launchOptions.query.cateId!='undefined'&&launchOptions.query.cateId!='null'){
        option.cateId = launchOptions.query.cateId || ''
      }
    this.setData({
      searchValue: option.title || '',
      cateId: option.cateId || ''
    })
    this.getList()
  },
  // 获取分类列表
  getList() {
    const { orderby_name, sort, convey_type, min_price, max_price } = this.data;
    getGoodsList({
      cate_id: this.data.cateId,
      goods_name: this.data.searchValue,
      orderby_name,
      sort,
      convey_type,
      min_price,
      max_price
    }).then(res => {
      my.stopPullDownRefresh()
      let newArr = []
      for (let key in res.data) {
        // if(res.data[key].id){
        newArr.push(res.data[key])
        // }
      }
      // newArr.forEach(item =>{
      //   const arr = item.rent_label.replace(/,/g, '，').split('，').filter((p, idx) => p != '' && idx < 2);
      //   item['labelList'] = arr
      // })
      newArr.forEach(item => {
        if (item.rent_label) {
          if (item.rent_label.indexOf("，") == 0) {
            let itemLable = item.rent_label.substr(1)
            if (itemLable.indexOf("，") == -1) {
              item['label1'] = itemLable
            } else {
              item.rent_label = itemLable.split('，')
              item['label1'] = item.rent_label[0]
              item['label2'] = item.rent_label[1]
              item['label3'] = item.rent_label[2]
            }
          } else {
            item.rent_label = item.rent_label.split(',')
            item['label1'] = item.rent_label[0]
            item['label2'] = item.rent_label[1]
            item['label3'] = item.rent_label[2]
          }
        }
      })
      this.setData({
        productList: newArr || []
      });
    }).catch(err =>{
      my.stopPullDownRefresh();
    })
  },
  selectCity() {
    const that = this
    my.chooseCity({
      showLocatedCity: true,
      showHotCities: true,
      setLocatedCity: true,
      success: (res) => {
              that.setData({
              topCity: res.city
            })
      },
    });
    // my.getLocation({
    //   type: 1,
    //   success(res) {
    //     that.setData({
    //       topCity: res.city.slice(0, 2)
    //     })
    //     my.chooseCity({
    //       // showLocatedCity: true,
    //       showHotCities: true,
    //       setLocatedCity: true,
    //       success: (res) => {
    //         that.setData({
    //           topCity: res.city
    //         })
    //       },
    //     });
    //   },
    // });
  },
  handleInput(value) {
    this.setData({
      searchValue: value,
    });
  },
  handleClear() {
    this.setData({
      searchValue: '',
    });
  },
  handleSubmit(value) {
    if (!value) {
      my.showToast({
        type: 'none',
        content: '请先输入关键词',
        duration: 1000,
      });
    } else {
      this.setData({
        searchValue: value,
      });
      this.getList()
    }
  },
  serchBtn() {
    if (!this.data.searchValue) {
      my.showToast({
        type: 'none',
        content: '请先输入关键词',
        duration: 1000,
      });
      return
    }
    this.getList()
  },
  goGoodsDetail(e) {
    my.navigateTo({
      url: '../productDetails/productDetails?goodsId=' + e.currentTarget.dataset.id+'&sale_user_id='+app.globalData.sale_user_id
    })
  },
  onSort() {
    this.setData({
      isSortTop: !this.data.isSortTop,
      isFilter: false
    })
  },
  onFilter() {
    this.setData({
      isFilter: !this.data.isFilter,
      isSortTop: false
    })
  },
  changeSort(e) {
    const { index, text } = e.currentTarget.dataset;
    let orderby_name = 'id', sort = 'desc';
    if (index == 1) {
      orderby_name = 'min_price';
      sort = 'desc';
    } else if (index == 2) {
      orderby_name = 'min_price';
      sort = 'asc';
    } else if (index == 3) {
      orderby_name = 'update_time';
      sort = 'desc';
    }
    this.setData({
      isSortTop: !this.data.isSortTop,
      tabsTitle: text,
      activeTabId_sort: index,
      orderby_name,
      sort
    })
    this.getList();
  },
  changeLogistic(e) {
    const { index, type } = e.currentTarget.dataset
    this.setData({
      activeTabId_logistic: index,
      logisticType: type
    })
  },
  rent_lowest(e) {
    this.setData({
      lowestRent: e.detail.value,
    });
  },
  rent_highest(e) {
    this.setData({
      highestRent: e.detail.value,
    });
  },
  filter_resets() {
    this.setData({
      activeTabId_logistic: -1,
      lowestRent: "",
      highestRent: "",
      convey_type: "",
      min_price: "",
      max_price: "",
      isFilter: false
    });
    this.getList();
  },
  filter_submits() {
    const that = this;
    const { logisticType, lowestRent, highestRent } = that.data;
    this.setData({
      convey_type: logisticType,
      min_price: lowestRent,
      max_price: highestRent,
      isFilter: false
    });
    this.getList();
  },
  onPopupClose() {
    this.setData({
      isSortTop: false,
      isFilter: false
    })
  },
  onPullDownRefresh() {
    this.getList()
  },
});
